using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using DAL;
using BUS;
using Utility;

public partial class Admin_TinhTrang_Add : System.Web.UI.Page
{
    int a;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
        }
        hpl_xoa.NavigateUrl = "~/Admin/TinhTrang_Del.aspx?id=" + ddl_matt.SelectedValue.ToString();
        xacnhan.Enabled = false; 
    }
    private void LoadDropDownList()
    {
        tbTinhTrangCollection ttCollection = tbTinhTrangBUS.GetAll();
        ddl_matt.DataValueField = tbTinhTrangColumns.matinhtrang.ToString();
        ddl_matt.DataSource = ttCollection;
        ddl_matt.DataBind();
        tbTinhTrang tt = tbTinhTrangBUS.GetByID(int.Parse(ddl_matt.SelectedValue));
        txt_tt.Text = tt.tentinhtrang;
    }
    private void Them()
    {
            tbTinhTrang tt = new tbTinhTrang();
            tt.tentinhtrang = txt_tt.Text;
            tbTinhTrangBUS.Insert(tt);       
        
    }
    private void Cap_Nhat()
    {
            tbTinhTrang ttr = new tbTinhTrang();
            ttr.matinhtrang = int.Parse(ddl_matt.SelectedValue);
            ttr.tentinhtrang = txt_tt.Text;
            tbTinhTrangBUS.Update(ttr);
    }
    private void Xoa()
    {
        int id = int.Parse(ddl_matt.SelectedValue);
        tbTinhTrang ttt = tbTinhTrangBUS.GetByID(id);
        tbTinhTrangBUS.Delete(id);
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        if ( a == 0 )
        {
            Them();
            hpl_xoa.Enabled = true;
            bt_add.Enabled = true;
            xacnhan.Enabled = false;
        }
        if (a == 1)
        {
            Cap_Nhat();
            bt_cn.Enabled = true;
            hpl_xoa.Enabled = true;
            bt_add.Enabled = true;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/SanPham_List.aspx");
    }
    protected void bt_add_Click(object sender, EventArgs e)
    {
        txt_tt.Enabled = true;
        bt_cn.Enabled = false;
        bt_add.Enabled = false;
        txt_tt.Text = "";
        xacnhan.Enabled = true;
        hpl_xoa.Enabled = false;
        a = 0;
    }
    protected void ddl_matt_SelectedIndexChanged(object sender, EventArgs e)
    {
        tbTinhTrang tt = tbTinhTrangBUS.GetByID(int.Parse(ddl_matt.SelectedValue));
        txt_tt.Text = tt.tentinhtrang;        
    }
    protected void bt_cn_Click(object sender, EventArgs e)
    {
        txt_tt.Enabled = true;
        a = 1;
        hpl_xoa.Enabled = false;
        bt_add.Enabled = false;
        bt_cn.Enabled = false;
        Label1.Enabled  = true;
        ddl_matt.Enabled = true;
    }
}
